package com.tozed.android.launcher.simpleui;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.RelativeLayout;

public class SimpleItemInfo
{
  public static final int NO_ID = -1;
  public long id = NO_ID;
  public long container = NO_ID;
  public int cellX = -1;
  public int cellY = -1;
  public int spanX = 1;
  public int spanY = 1;
  public int screen = -1;
  public int itemType;  
  public String iconPackage = null;
  public String iconResource = null;
  public Intent intent;
  public boolean isFixedPosition = false;
  boolean isGesture;
  public Bitmap mIcon;
  public int mUnReadNum = 0;
  
  public CharSequence title;
  public RelativeLayout view;
  
	SimpleItemInfo(){
	}

	SimpleItemInfo(ResolveInfo resolveInfo){
		String str = resolveInfo.activityInfo.applicationInfo.packageName;
		this.intent = new Intent(Intent.ACTION_MAIN);
		this.intent.addCategory(Intent.CATEGORY_LAUNCHER);
		this.intent.setComponent(new ComponentName(str, resolveInfo.activityInfo.name));
		this.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
	}

	SimpleItemInfo(SimpleItemInfo itemInfo){
		this.id = itemInfo.id;
		this.cellX = itemInfo.cellX;
		this.cellY = itemInfo.cellY;
		this.spanX = itemInfo.spanX;
		this.spanY = itemInfo.spanY;
		this.screen = itemInfo.screen;
		this.itemType = itemInfo.itemType;
		this.container = itemInfo.container;
	}
	
	// add fields to database
	void onAddToDatabase(ContentValues values) { 
		values.put("itemType", Integer.valueOf(itemType));
	    if (!isGesture){
	    	values.put("container", Long.valueOf(container));
	    	values.put("screen", Integer.valueOf(screen));
	    	values.put("cellX", Integer.valueOf(cellX));
	    	values.put("cellY", Integer.valueOf(cellY));
	    	values.put("spanX", Integer.valueOf(spanX));
	    	values.put("spanY", Integer.valueOf(spanY));
	    	values.put("intent", intent.toUri(0));
	    }
	    
	    // 如果是快捷方式把图标存数据库中
	    if (itemType == SimpleLauncherSettings.SimpleFavorites.ITEM_TYPE_SHORTCUT)
	      writeBitmap(values, mIcon);
	}
	
	static void writeBitmap(ContentValues values, Bitmap bitmap) {
        if (bitmap != null) {
            byte[] data = flattenBitmap(bitmap);
            values.put(SimpleLauncherSettings.SimpleFavorites.ICON, data);
        }
    }
	
	static byte[] flattenBitmap(Bitmap bitmap) {
        int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        ByteArrayOutputStream out = new ByteArrayOutputStream(size);
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
            Log.w("SimpleFavorite", "Could not write icon");
            return null;
        }
    }
	
	public ComponentName getComponentName(){
		if (intent == null)
			return null;
		return intent.getComponent();
	}
	
	public String getClsName(){
		if (intent == null)
			return null;
		
		ComponentName componentName = intent.getComponent();
		if (componentName == null) 
			return null;
		
		return componentName.getClassName();
	}
	
	public String getPkgName(){
		if (intent == null)
			return null;
		
		ComponentName componentName = intent.getComponent();
		if (componentName == null) 
			return null;
		
		return componentName.getPackageName();
	}

	public int getUnReadNum(){
		return this.mUnReadNum;
	}

	RelativeLayout getView(){
		return this.view;
	}

	void setIcon(Bitmap bitmap){
		this.mIcon = bitmap;
	}

	public void setShortcutPosition(int cellX, int cellY, int screen){
		this.cellX = cellX;
		this.cellY = cellY;
		this.screen = screen;
	}

	public void setUnReadNum(int unReadNum){
		this.mUnReadNum = unReadNum;
	}

	void setView(RelativeLayout view){
		this.view = view;
	}
	
	public static boolean canUninstall(PackageManager pm, String packageName){
		// implement
		if (packageName == null || packageName.equals(""))
			return false;
		
		int flags;
		try{
			flags = pm.getApplicationInfo(packageName, 0).flags;
		} catch (NameNotFoundException e){
			e.printStackTrace();
			return false;
		} catch (Exception ex){
			ex.printStackTrace();
			return false;
		}
		
		// 持久属性应用不能卸载
		if ((flags & ApplicationInfo.FLAG_PERSISTENT) > 0)
			return false;
		
		// 系统应用不能卸载
		if ((flags & ApplicationInfo.FLAG_SYSTEM) > 0
			|| (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) > 0)
			return false;
		
		return true;
	}
	
	public boolean isBelongToDownloadFolder(PackageManager pm){
		String pkgName = getPkgName();
		// 内置应用过虑,返回true则在下载文件夹中
		return canUninstall(pm, pkgName);
	}
	
	void unbind() {
		
    }
	
	public String toString()
	{
		return "SimpleItem(id=" + this.id + " type=" + this.itemType + " container=" + this.container 
				+ " screen=" + this.screen + " cellX=" + this.cellX + " cellY=" + this.cellY 
				+ " spanX=" + this.spanX + " spanY=" + this.spanY + " isFixedPosition=" 
				+ this.isFixedPosition + " dropPos=" + ")";
	}
    
	
  
}
